package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.dao.GonggaoDao;
import com.dao.LabDao;
import com.dao.LiuyanDao;
import com.dao.UserDao;
import com.dao.YuyueDao;
import com.model.Gonggao;
import com.model.Lab;
import com.model.Liuyan;
import com.model.User;
import com.model.Yuyue;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Util;

public class RegesterAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
            
	
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	private LabDao ld;
	public void setLd(LabDao ld) {
		this.ld = ld;
	}
	
	private UserDao ud;
	public void setUd(UserDao ud) {
		this.ud = ud;
	}

	private YuyueDao yd;
	public void setYd(YuyueDao yd) {
		this.yd = yd;
	}
	
	private  LiuyanDao liuyandao;
	
	

	public LiuyanDao getLiuyandao() {
		return liuyandao;
	}
	public void setLiuyandao(LiuyanDao liuyandao) {
		this.liuyandao = liuyandao;
	}

	private GonggaoDao gd;
	
	
	public GonggaoDao getGd() {
		return gd;
	}
	public void setGd(GonggaoDao gd) {
		this.gd = gd;
	}
	// 获取请求对象
	public HttpServletRequest getRequest() {
			HttpServletRequest request = ServletActionContext.getRequest();
			return request;
		}

		// 获取响应对象
    public HttpServletResponse getResponse() {
			HttpServletResponse response = ServletActionContext.getResponse();
			return response;
		}

		// 获取session对象
   public HttpSession getSession() {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			return session;
		}
   
	
   
 //登陆操作
 	public String login() throws Exception {
 		
 		HttpServletRequest request = this.getRequest();
 		String username = request.getParameter("username");
 		String password = request.getParameter("password");
 		String  role = request.getParameter("role");
 		System.out.println("登陆Action");
 		User user = ud.selectBean(" where username = '" + username
 				+ "' and password= '" + password + "' and role= "+role +" and deletestatus=0 ");
 		if (user != null) {
 			JSONObject jsonob=new JSONObject();
 			jsonob.put("code", 1);
 			jsonob.put("user", user);
 			getResponse().getWriter().print(jsonob.toString());
 		} else{
 			JSONObject jsonob1=new JSONObject();
 			jsonob1.put("code", 2);
 			jsonob1.put("msg", "用户名或密码错误！");
 			getResponse().getWriter().print(jsonob1.toString());
 		}
 		return null;
 	}
   
   
   
	
// 得到所有学生钢琴房的JSON数据
	public void fenleiStu() throws Exception {
		List<Lab> list = ld.selectStuLab();
		// JSONArray jsonarray=new JSONArray();
		// jsonarray.add(list);
		getRequest().setCharacterEncoding("UTF-8");
		getResponse().setContentType("text/html;charset=UTF-8");
		getResponse().getWriter().print(JSONArray.fromObject(list).toString());

	}
	
	
	// 得到所有教师钢琴房的JSON数据
		public void fenleiTea() throws Exception {
			List<Lab> list = ld.selectTeaLab();
			getRequest().setCharacterEncoding("UTF-8");
			getResponse().setContentType("text/html;charset=UTF-8");
			getResponse().getWriter().print(JSONArray.fromObject(list).toString());

		}
	
	
		// 得到某个用户的所有的预约表
		public void yuYueUser() throws Exception {
			getRequest().setCharacterEncoding("UTF-8");
			getResponse().setContentType("text/html;charset=UTF-8");
			
			String userId=getRequest().getParameter("userId");
			
			List<Yuyue> list = yd.selectYuYue(Integer.parseInt(userId));
		
			getResponse().getWriter().print(JSONArray.fromObject(list).toString());
			return;

		}
		
		
		
		
		//删除预约
			public void yuyuedelete() throws IOException {
				  HttpServletResponse  response=this.getResponse();
				   response.setCharacterEncoding("UTF-8");
					HttpServletRequest request = this.getRequest();
					Yuyue bean = yd.selectBean(" where id= "+ request.getParameter("id"));
					bean.setDeletestatus(1);
					bean.setShenhe("未通过");
					yd.updateBean(bean);
				    
					
					JSONObject jsonob=new JSONObject();
					jsonob.put("code", 2);
				    jsonob.put("msg", "已取消");
					getResponse().getWriter().print(jsonob.toString());
				    
				}
		
		
		//修改密码
		public void changepwd2() throws IOException {
			HttpServletRequest request = this.getRequest();
			
		     String userid=  request.getParameter("userid");
			String password1 = request.getParameter("password1");
			String password2 = request.getParameter("password2");
			User u=ud.selectBean("where id="+userid);
			User bean = ud.selectBean(" where username= '"+u.getUsername()+"' and password= '"+password1+"' and deletestatus=0");
			if(bean!=null){
				bean.setPassword(password2);
				ud.updateBean(bean);
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 1);
				getResponse().getWriter().print(jsonob.toString());
			}else{
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 2);
				getResponse().getWriter().print(jsonob.toString());
			}
		}
		
		
		public   void preyuyueadd() throws IOException{
			HttpServletRequest request = this.getRequest();
	        HttpServletResponse  response=this.getResponse();
	        response.setCharacterEncoding("UTF-8");
			//夏天时间段
	        List<String> list=new ArrayList<String>();
	        list.add("上午6点到8点");
	        list.add("上午8点到10点");
	        list.add("上午10点到12点");
	        list.add("下午2点到4点");
	        list.add("下午4点到6点");
	        list.add("晚上6点到8点");
	        list.add("晚上8点到10点");
		
	         String  riqi=request.getParameter("riqi");
	         
	      //   String  labid= request .getParameter("labid");
	         String  lab_name= request .getParameter("lab_name");
	      //   System.out.println(riqi+"...."+labid+"");
	       //  Lab lab=ld.selectBean(" where id= "+ Integer.parseInt(labid));
	         Lab lab=ld.selectBean(" where mingchen='"+lab_name+"'");
	         System.out.println(lab.getFenlei());
	          int labid=lab.getId();
	         System.out.println("labid="+lab.getId());
	         //根据该钢琴房和日期查询预约表
	         List<Yuyue> list1 = yd.selectBeanList(0, 9999, " where deletestatus=0  and shenhe='审核通过' and riqi='"+riqi+"' and lab.id='"+labid+"' ");
	      
	         System.out.println("list1.size="+list1.size());
	         for(Yuyue bean:list1){
	           String sjd= bean.getSjd();
	           if(sjd.equals("上午6点到8点")){
	        	   list.remove("上午6点到8点");
	           }
	           if(sjd.equals("上午8点到10点")){
	    		   list.remove("上午8点到10点");
	    	   }
	    	   if(sjd.equals("上午10点到12点")){
	    		   list.remove("上午10点到12点");
	    	   }
	    	   if(sjd.equals("下午2点到4点")){
	    		   list.remove("下午2点到4点");
	    	   }
	    	   if(sjd.equals("下午4点到6点")){
	    		   list.remove("下午4点到6点");
	    	   }
	    	   if(sjd.equals("晚上6点到8点")){
	    		   list.remove("晚上6点到8点");
	    	   }
	    	   if(sjd.equals("晚上8点到10点")){
	    		   list.remove("晚上8点到10点");
	    	   }
	         }
	         JSONObject jsonob=new JSONObject();
				jsonob.put("list", list);
				getResponse().getWriter().print(jsonob.toString());
				return;
	         
		}
		
		//教师预约
		public void yuyueadd2() throws Exception {
			HttpServletRequest request = this.getRequest();
	        HttpServletResponse  response=this.getResponse();
	        response.setCharacterEncoding("UTF-8");
		//	PrintWriter  writer = this.getPrintWriter();
			String menu = request.getParameter("menu");
			
			String jieshu = request.getParameter("jieshu");//结束使用时间2016-06-07 08:00
			String labid = request.getParameter("labid");
			String shiyong = request.getParameter("shiyong");//开始使用时间2016-06-07 08:00
			String shuoming = request.getParameter("shuoming");
			String userId = request.getParameter("userId");
			Lab lab=ld.selectBean(" where id= "+ Integer.parseInt(labid));
	        User user=ud.selectBean("where id="+Integer.parseInt(userId));
			long t1 = Util.parseTime(shiyong).getTime();//开始使用时间
			
			long t2 = Util.parseTime(jieshu).getTime();//结束使用时间

			if(t1>=t2){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 1);
				jsonob.put("menu", menu);
				jsonob.put("msg", "操作失败，开始使用时间必须小于结束时间");
				 response.setCharacterEncoding("UTF-8");
				getResponse().getWriter().print(jsonob.toString());
			
				return ;
			}
			
			long now = new Date().getTime();
			
			if(t1<=now){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 2);
				jsonob.put("menu", menu);
				jsonob.put("msg", "操作失败，开始使用时间必须大于当前时间");
				getResponse().getWriter().print(jsonob.toString());
			
				return ;
			}
			
			//...............判断这个星期几用户预约那天能不能预约
			String weekname=Util.getweekname(shiyong);//得到预约的时间是星期几
			int weekone=user.getMonday();
			int weektwo= user.getTuesday(); 
			int weekthree= user.getWednesday();
			int weekfour=user.getThursday();
	    	int weekfive=	 user.getFriday();
	 	    int weeksix=user.getSaturday();
	 	    int weekseven=user.getSaturday();
			int flaga=0;
			if(weekname.equals("星期一")){
				 if(weekone==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期二")){
				 if(weektwo==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期三")){
				 if(weekthree==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期四")){
				 if(weekfour==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期五")){
				 if(weekfive==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期六")){
				 if(weeksix==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期日")){
				 if(weekseven==1){
					flaga=1;
			
				}
			}
			
			
			if(flaga==1){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 3);
				jsonob.put("menu", menu);
				jsonob.put("msg", "对不起，你不能预约"+weekname+"");
				getResponse().getWriter().print(jsonob.toString());
				return;
		
			}
			
			
			//。。。。。判断这个今天该钢琴房开不开放
		//	String weekname=	Util.getweekname(shiyong);
			int weekone1=lab.getMonday();
			int weektwo1= lab.getTuesday(); 
			int weekthree1= lab.getWednesday();
			int weekfour1=lab.getThursday();
	    	int weekfive1=lab.getFriday();
	 	    int weeksix1=lab.getSaturday();
	 	    int weekseven1=lab.getSaturday();
			int flagb=0;
			if(weekname.equals("星期一")){
				 if(weekone1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期二")){
				 if(weektwo1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期三")){
				 if(weekthree1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期四")){
				 if(weekfour1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期五")){
				 if(weekfive1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期六")){
				 if(weeksix1==1){
					 flagb=1;
			
				}
			}
			if(weekname.equals("星期日")){
				 if(weekseven1==1){
					 flagb=1;
			
				}
			}
			
			
			if(flagb==1){
				
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 4);
			//	jsonob.put("menu", menu);
				jsonob.put("msg", "该钢琴房"+weekname+"不开放");
				getResponse().getWriter().print(jsonob.toString());
				return;
				//writer.print("<script language=javascript>alert('对不起，该钢琴房今天不开放');window.location.href='method!yuyueadd?menu="+menu+"&labid="+labid+"';</script>");
			}
			
			
			
			
			
			
		//............	   
		int  xiaoshi=user.getXiaoshi();
		long shijiancha;
		shijiancha=Util.shijiancha(shiyong, jieshu);
		long dd= shijiancha-xiaoshi*60;
		if(dd>0){
			JSONObject jsonob=new JSONObject();
			jsonob.put("code", 5);
			jsonob.put("menu", menu);
			jsonob.put("msg", "操作失败，一次不能超过"+user.getXiaoshi()+"小时");
			response.setCharacterEncoding("UTF-8");
			 getResponse().getWriter().print(jsonob.toString());
			//writer.print("<script language=javascript>alert('操作失败，一次不能预约这么长时间');window.location.href='method!yuyueadd?menu="+menu+"&labid="+labid+"';</script>");
			return ;
		}
		
		
			
			
			//先取出所有的审核通过且没过期的钢琴房
			List<Yuyue> list2 = yd.selectBeanList(0, 9999," where deletestatus=0 and lab.id="+labid+" and shenhe='审核通过' and j1>"+now+" and user.id=  "+user.getId());
			
			int flag = 0;
			for(Yuyue bean:list2){
				if(bean.getS1()>=t1&&bean.getS1()<=t2&&bean.getJ1()>=t2){
					flag = 1;
					break;
				}
				
				if(bean.getS1()<=t1&&bean.getJ1()>=t1&&bean.getJ1()<=t2){
					flag = 1;
					break;
				}
				
				
				if(bean.getS1()<=t1&&bean.getJ1()>=t2){
					flag = 1;
					break;
				}
				
				if(bean.getS1()>=t1&&bean.getJ1()<=t2){
					flag = 1;
					break;
				}
				
			}
			
			if(flag==1){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 6);
				jsonob.put("menu", menu);
				jsonob.put("msg", "操作失败，在该时间段内您已提交钢琴房预约");
				getResponse().getWriter().print(jsonob.toString());
			//	writer.print("<script language=javascript>alert('操作失败，在该时间段内您已提交钢琴房预约，正在审核中');window.location.href='method!yuyueadd?menu="+menu+"&labid="+labid+"';</script>");
				return ;
			}
			
			
			//判断该时间段有没有成功预约的钢琴房
			
			//先取出所有的审核通过且没过期的钢琴房
			List<Yuyue> list = yd.selectBeanList(0, 9999," where deletestatus=0 and lab.id="+labid+" and shenhe='审核通过' and j1>"+now+"   ");
			
			flag = 0;
			for(Yuyue bean:list){
				if(bean.getS1()>=t1&&bean.getS1()<=t2&&bean.getJ1()>=t2){
					flag = 1;
					break;
				}
				
				if(bean.getS1()<=t1&&bean.getJ1()>=t1&&bean.getJ1()<=t2){
					flag = 1;
					break;
				}
				
				
				if(bean.getS1()<=t1&&bean.getJ1()>=t2){
					flag = 1;
					break;
				}
				
				if(bean.getS1()>=t1&&bean.getJ1()<=t2){
					flag = 1;
					break;
				}
			}
			
			if(flag==1){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 7);
				jsonob.put("menu", menu);
				jsonob.put("msg", "操作失败，在该时间段内该钢琴房已被预订");
				getResponse().getWriter().print(jsonob.toString());
			//	writer.print("<script language=javascript>alert('操作失败，在该时间段内该钢琴房已被预订');window.location.href='method!yuyueadd?menu="+menu+"&labid="+labid+"';</script>");
				return ;
			}
			
			//。。。。。。。。。。。。一天可预约几次功能
			
			 int dayofnum=	user.getDayofnum();//获得一天可预约次数
			 String nowriqi=Util.getRiqi();//得到当前日期
			 
			// long nowriqi1=Util.parseTime1(nowriqi).getTime();
			
			     int userid=user.getId();
				 List<Yuyue> list1 = yd.selectBean1("where deletestatus=0  and shenhe='审核通过' and shijian='"+nowriqi+"' and userid='"+userid+"' ");
				 
				 if(dayofnum<= list1.size()){
					 JSONObject jsonob=new JSONObject();
					  jsonob.put("code",8);
						jsonob.put("menu", menu);
						jsonob.put("msg", "操作失败,一天你最多预约"+dayofnum+"次");
						getResponse().getWriter().print(jsonob.toString());
				
					return ;
				}
				 
			//	 
			int weekofnum=user.getWeekofnum();//获得一周可预约次数
			
			
			Yuyue bean = new Yuyue();
			
			
			bean.setJieshu(jieshu);
			bean.setLab(ld.selectBean(" where id= "+labid));
			bean.setShenhe("审核通过");
			bean.setShijian(Util.getTime());
			bean.setShiyong(shiyong);
			bean.setShuoming(shuoming);
			bean.setUser(user);
			bean.setJ1(t2);
			bean.setS1(t1);
			
			
			
			yd.insertBean(bean);
			JSONObject jsonob=new JSONObject();
			jsonob.put("code", 9);
			jsonob.put("menu", menu);
			jsonob.put("msg", "操作成功");
			getResponse().getWriter().print(jsonob.toString());
			
			
			
			
		}
	 
		 
		//学生预约
		public void yuyueadd20() throws Exception {
			HttpServletRequest request = this.getRequest();
		    HttpServletResponse response= this.getResponse();
		 	 response.setCharacterEncoding("UTF-8");
		 	
			String sjd = request.getParameter("sjd");//
		//	String labid = request.getParameter("labid");
			String lab_name = request.getParameter("lab_name");
			String riqi = request.getParameter("riqi");//
		//	String shuoming = request.getParameter("shuoming");
			String userId=request.getParameter("userid");
			 
			 Lab lab=ld.selectBean(" where mingchen='"+lab_name+"'");
			 int labid=lab.getId();
		
		     if(sjd.equals("该钢琴房已满，不能再预约")){
		    	 JSONObject jsonob=new JSONObject();
					jsonob.put("code", 0);
				    jsonob.put("msg", "该钢琴房已满，不能再预约");
					getResponse().getWriter().print(jsonob.toString());
					return;
			}
		     
			//Lab lab=ld.selectBean(" where id= "+ Integer.parseInt(labid));
		    
	        User user=ud.selectBean("where id="+Integer.parseInt(userId));
			//获得一天可预约次数
		    int dayofnum=	user.getDayofnum();
	
		    //获得一周可预约次数
		     int weekofnum=user.getWeekofnum();
		
	       
		    
		    //得到预约日期
			String time = "";
			if("上午6点到8点".equals(sjd)){
				time= riqi+" 06:00";
			}
			if("上午8点到10点".equals(sjd)){
				time= riqi+" 08:00";
			}
			if("上午10点到12点".equals(sjd)){
				time= riqi+" 10:00";
			}
			if("下午2点到4点".equals(sjd)){
				time= riqi+" 14:00";
			}
			if("下午4点到6点".equals(sjd)){
				time= riqi+" 16:00";
			}
			if("晚上6点到8点".equals(sjd)){
				time= riqi+" 18:00";
			}
			if("晚上8点到10点".equals(sjd)){
				time= riqi+" 20:00";
			}
			
			long t1 = Util.parseTime(time).getTime();
			
			
			
			long now = new Date().getTime();
			
			if(t1<=now){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 1);
			    jsonob.put("msg", "操作失败，预约时间必须大于当前时间");
				getResponse().getWriter().print(jsonob.toString());
				return ;
			}
			
		    int weekshu=Util.parserweek();//获取当前周数
		   int yuyueweekshu= Util.parserweek1(riqi);//预约的周数
		    int weekzhoushu=weekshu+1;
		   if(yuyueweekshu>weekzhoushu){
			   JSONObject jsonob=new JSONObject();
				jsonob.put("code", 11);
			    jsonob.put("msg", "只能预约本周和下周");
				getResponse().getWriter().print(jsonob.toString());
				return ;
		   }
             
		
			String weekname=	Util.getweekname(time);//得到预约的时间是星期几
			int weekone=user.getMonday();
			int weektwo= user.getTuesday(); 
			int weekthree= user.getWednesday();
			 int weekfour=user.getThursday();
	    	int weekfive=	 user.getFriday();
	 	    int weeksix=user.getSaturday();
	 	    int weekseven=user.getSaturday();
			int flaga=0;
			if(weekname.equals("星期一")){
				 if(weekone==1){
					flaga=1;
			                    }
			}
			if(weekname.equals("星期二")){
				 if(weektwo==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期三")){
				 if(weekthree==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期四")){
				 if(weekfour==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期五")){
				 if(weekfive==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期六")){
				 if(weeksix==1){
					flaga=1;
			
				}
			}
			if(weekname.equals("星期日")){
				 if(weekseven==1){
					flaga=1;
			
				}
			}
			
			
			if(flaga==1){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 2);
			    jsonob.put("msg", "操作失败，你不能预约"+weekname);
				getResponse().getWriter().print(jsonob.toString());
				return;
				
			}
			
			
			
			//。。。。。判断这个今天该钢琴房开不开放
			
				int weekone1=lab.getMonday();
				int weektwo1= lab.getTuesday(); 
				int weekthree1= lab.getWednesday();
				int weekfour1=lab.getThursday();
		    	int weekfive1=lab.getFriday();
		 	    int weeksix1=lab.getSaturday();
		 	    int weekseven1=lab.getSaturday();
				int flagb=0;
				if(weekname.equals("星期一")){
					 if(weekone1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期二")){
					 if(weektwo1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期三")){
					 if(weekthree1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期四")){
					 if(weekfour1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期五")){
					 if(weekfive1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期六")){
					 if(weeksix1==1){
						 flagb=1;
				
					}
				}
				if(weekname.equals("星期日")){
					 if(weekseven1==1){
						 flagb=1;
				
					}
				}
				
				
				if(flagb==1){
					
					JSONObject jsonob=new JSONObject();
					jsonob.put("code", 3);
				
					jsonob.put("msg", "该钢琴房"+weekname+"不开放");
					getResponse().getWriter().print(jsonob.toString());
					return;
					
				}
			
			
			
			
			
			/**
			 * 查看用户是否该日期、时间段是否已预约
			 */
			Yuyue yuyue = yd.selectBean(" where deletestatus=0 and user.id="+user.getId()+" and shenhe='审核通过' and riqi='"+riqi+"' and sjd='"+sjd+"' ");
			if(yuyue!=null){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 4);
			    jsonob.put("msg", "该时间段你已提交预约，不能再预约");
				getResponse().getWriter().print(jsonob.toString());
			    return;
			}
			
	
			
			/**
			 * 查看钢琴房是否已被预约
			 */
			String fenlei = null;
			int  role=user.getRole();
			if(role==1){
				fenlei="学生钢琴房";
				
			}
			if(role==2){
				fenlei="教师钢琴房";
			}
			
			List<Yuyue> list = yd.selectBeanList(0, 9999, " where deletestatus=0  and shenhe='审核通过'and lab.fenlei='"+fenlei+"' and riqi='"+riqi+"' and sjd='"+sjd+"' ");
		     	if(list.size()>0){
			//if(list.size()>=lab.getRenshu()){
				JSONObject jsonob=new JSONObject();
				jsonob.put("code", 5);
			
				jsonob.put("msg", "该时间段已被预约");
				
				getResponse().getWriter().print(jsonob.toString());
				
				return;
			}
			
			
			
			 
		    /**
		     * 一天不能超过几次操作
		     */
			//得到当前日期
			 String nowriqi=Util.getRiqi();
			
			
			 int userid=user.getId();
				 List<Yuyue> list1 = yd.selectBean1("where deletestatus=0  and shenhe='审核通过' and riqi='"+riqi+"' and userid='"+userid+"' ");
				 
				 if(dayofnum<= list1.size()){
					 JSONObject jsonob=new JSONObject();
						jsonob.put("code", 6);
					    jsonob.put("msg", "一天最多预约"+dayofnum+"次");
						getResponse().getWriter().print(jsonob.toString());
						return ;
				}
				
			
			
			 /**
			  * 一周不能超过几次操作
			  */
			int  nowweek= Util.parserweek();//得到当前周数
			int nowweek1=Util.parserweek1(riqi);
		
			 List<Yuyue> list2 = yd.selectBean2("where deletestatus=0  and shenhe='审核通过' and weeknum='"+nowweek1+"' and userid='"+userid+"' ");
		    
		    	if(weekofnum <=list2.size()){
		    		 JSONObject jsonob=new JSONObject();
						jsonob.put("code", 7);
					    jsonob.put("msg", "一周最多预约"+weekofnum+"次");
					    getResponse().getWriter().print(jsonob.toString());
		    		return ;
		    		}
		    
		    		
			 
			 
			
			
			
			
			
			
			
			
			Yuyue bean = new Yuyue();
			bean.setLab(lab);
			bean.setShenhe("审核通过");
			bean.setShijian(Util.getTime());
		//	bean.setShuoming(shuoming);
			bean.setUser(user);
			bean.setSjd(sjd);
			bean.setRiqi(riqi);
			bean.setWeeknum(nowweek1);
			
			yd.insertBean(bean);
			
			
		    
			JSONObject jsonob=new JSONObject();
			jsonob.put("code", 8);
		    jsonob.put("msg", "预约成功");
		    getResponse().getWriter().print(jsonob.toString());
			
			
			
		}
		
		
		
		
		
		
		//得到所有的公告
		 public void gonggaolist()throws Exception{
		      List<Gonggao> list=	gd.selectAllGonggao();
		      getRequest().setCharacterEncoding("UTF-8");
				getResponse().setContentType("text/html;charset=UTF-8");
			getResponse().getWriter().print(JSONArray.fromObject(list).toString()); 
			    	return ;
		     }
		 
		
		 //查找某个用户的所有留言
	     public void liuyanlist()throws Exception{
	    	 HttpServletRequest request = this.getRequest();

	    	 String userId=(String) request.getParameter("userid");
	    	 int Id=Integer.parseInt(userId);
	    	 List<Liuyan> list= liuyandao.seletctBeanList(Id);
	    	 getRequest().setCharacterEncoding("UTF-8");
			 getResponse().setContentType("text/html;charset=UTF-8");
			 if(list!=null){
		     getResponse().getWriter().print(JSONArray.fromObject(list).toString());
			 }
		     return ;
	     }
		 
	    //上传留言
	     public void liuyanadd() throws IOException {
				HttpServletRequest request = this.getRequest();
			
				String userId = request.getParameter("userId");
			    String ltitle = request.getParameter("ltitle");
				String neirong = request.getParameter("neirong");
			    int	userid=Integer.parseInt(userId);
				User user=ud.selectBean("where id="+userId);
				
				
				Liuyan bean = new Liuyan();
				bean.setLtitle(ltitle);
				bean.setNeirong(neirong);
				bean.setShijian(Util.getTime());
				bean.setUser(user);
				bean.setZhuangtai("未回复");
				
				liuyandao.insertBean(bean);
				JSONObject jo=new JSONObject();
				jo.put("code",1 );
				getResponse().getWriter().print(jo.toString());
				
				
	}
	     
}
